package de.koller.worlders.engine.entity.zone;

import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.engine.tiles.ITile;
import de.koller.worlders.engine.tiles.data.ITileCreator;
import de.koller.worlders.render.VisualMap;
import de.koller.worlders.render.visual.DisplayRenderObject;
import java.util.List;
import java.util.Random;

public class Portal extends ZoneEffect {
	public static final int GEN_ID = 13;
	public static final int PORTALS = 4;
	private static final Random rnd = new Random();
	
	public static final ITileCreator GEN_INSTANCE = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new Portal( 0 ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.portal0; }
		@Override public String getName() { return "Portal 1"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_1 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new Portal( 1 ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.portal1; }
		@Override public String getName() { return "Portal 2"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_2 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new Portal( 2 ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.portal2; }
		@Override public String getName() { return "Portal 3"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_3 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new Portal( 3 ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.portal3; }
		@Override public String getName() { return "Portal 4"; }
	};
	
	private final int type;

	public Portal( int type ) {
		this.type = type;
	}
	
	@Override
	public void init( Zone z ) {
		super.init( z );

//		z.setTextureRep( new TextureRep( "data/tiles.png", type*32, 64, 32, 32 ) );
		if( type == 0 ) z.getTile().setVisual( VisualMap.portal0 );
		if( type == 1 ) z.getTile().setVisual( VisualMap.portal1 );
		if( type == 2 ) z.getTile().setVisual( VisualMap.portal2 );
		if( type == 3 ) z.getTile().setVisual( VisualMap.portal3 );
		
		if( z.root == null )
			return;
		
		z.root.getEntityContainer().getPortalEntities().getPortals( type ).add( this );
	}

	@Override
	public void release() {
		super.release();
		
		zone.root.getEntityContainer().getPortalEntities().getPortals( type ).remove( this );
	}

	@Override
	public void enter( MovingObject mo ) {
		super.enter( mo );
		
		if( mo.getPortalImmune() == this ) {
			mo.setPortalImmune( null );
			return;
		}
		
		List<Portal> l = zone.root.getEntityContainer().getPortalEntities().getPortals( type );
		if( l.size() < 2 )
			return;
		
		Portal p;
		do {
			p = l.get( rnd.nextInt( l.size() ) );
		} while( p == this );
		
		portTo( mo, p );
	}

	private void portTo( MovingObject mo, Portal portal ) {
		float x; float y;
		x = portal.zone.getPosition().getXf() + .5f;
		y = portal.zone.getPosition().getYf() + .5f;
		x -= mo.getSize().getXf() * .5f;
		y -= mo.getSize().getYf() * .5f;
		
		mo.getPosition().set( x, y );
		mo.setPortalImmune( portal );
	}
}

